#Replication Code for IO Appendix (6.28.2023)

library(tidyverse)
library(skimr)
library(gplots)
library(broom)
library(dotwhisker)
library(lubridate)
library(lavaan)
library(jsonlite)
library(mediation)
library(psych)
library(ltm)
library(pwr)

## Quick fix for stargazer <= 5.2.3 is.na() issue with long model names in R >= 4.2
# Unload stargazer if loaded
detach("package:stargazer",unload=T)
# Delete it
remove.packages("stargazer")
# Download the source
download.file("https://cran.r-project.org/src/contrib/stargazer_5.2.3.tar.gz", destfile = "stargazer_5.2.3.tar.gz")
# Unpack
untar("stargazer_5.2.3.tar.gz")
# Read the sourcefile with .inside.bracket fun
stargazer_src <- readLines("stargazer/R/stargazer-internal.R")
# Move the length check 5 lines up so it precedes is.na(.)
stargazer_src[1990] <- stargazer_src[1995]
stargazer_src[1995] <- ""
# Save back
writeLines(stargazer_src, con="stargazer/R/stargazer-internal.R")
# Compile and install the patched package
install.packages("stargazer", repos = NULL, type="source")

library(stargazer)


# LOADING DATA
load(file = "final_data_IO.Rdata")


#SECTION A.2 RESPONDENT QUALITY

#Number of complete responses (n=5499)
final_data_IO %>% count(complete)


#SECTION A.3 POWER ANALYSIS

#Statistical Power Test for linear model 
#where u and v are the numerator and denominator degrees of freedom. We use f2 as the effect size measure.
#u = number of variables in the model + the intercept - 1
#v = number of error degrees of freedom, which is n - u
#Cohen (1992) suggests f 2 values of 0.02, 0.15, and 0.35 represent small, medium and larger effect sizes, respectively

pwr.f2.test(u = 4, v = , f2 = .02, sig.level = .05, power = .8)


#Statistical Power Test for T-Tests (different sample size in each group) [control only]
#where n is the sample size, d is the effect size, and type indicates a two-sample t-test, one-sample t-test or paired t-test. 
#Cohen (1992) suggests that d values of 0.2, 0.5, and 0.8 represent small, medium, and large effect sizes, respectively

pwr.t.test(n = , d = .2, sig.level = .05, power = .8, type = c("two.sample"))


#Statistical Power Test for one-way ANOVA
#where k is the number of groups and n is the common sample size in each group.
#Cohen (1992) suggests that d values of .1 .25 .4 represent small, medium, and large effect sizes, respectively

pwr.anova.test(k = 4, n = , f = .1, sig.level = .05, power = .8)


#SECTION A.4 DESCRIPTIVE STATISTICS

#Table A.1: Descriptive statistics of sociodemographic variables (complete surveys)
#Gender breakdown 
final_data_IO %>%
  filter(complete == 1) %>% 
  count(male)

#Ethnicity breakdown 
final_data_IO %>%
  filter(complete == 1) %>% 
  count(white)

#Religion breakdown 
final_data_IO %>%
  filter(complete == 1) %>% 
  count(christian)

#Rural breakdown 
final_data_IO %>%
  filter(complete == 1) %>% 
  count(rural)

#Region breakdown 
final_data_IO %>%
  filter(complete == 1) %>% 
  count(region)

#PID breakdown 
final_data_IO %>%
  filter(complete == 1) %>% 
  count(pid_group_merged)

#Vote2020 breakdown 
final_data_IO %>%
  filter(complete == 1) %>% 
  count(trump2020)

#Political interest 
final_data_IO %>%
  filter(complete == 1) %>% 
  count(polint)


#SECTION C.5 IMPACT

#Load data from pilot study of news memorability

load(file = "memorypilot_data_IO.Rdata")

#Recall topic of article?
memorypilot_data_IO %>% count(recall_topic)

#Recall News Organization?
memorypilot_data_IO %>% count(recall_org)
memorypilot_data_IO %>% count(treatment, recall_org)

#39 of the people who said "Yes" couldn't actually remember
memorypilot_data_IO %>% group_by(treatment, Q44_1_TEXT) %>% count %>% print(n=1000)

#Recall News Organization when given choices?
memorypilot_data_IO %>% count(recall_org2)

#Recall News Reporter Names?
memorypilot_data_IO %>% count(recall_reporters)

#Of the people who said yes, only two could produce the name: Peter Kenyon and Maria Montez (should have been Martinez)
memorypilot_data_IO %>% group_by(treatment, Q46_1_TEXT) %>% count %>% print(n=1000)

#Recall News Reporter Names when given choices?
memorypilot_data_IO %>% count(recall_reporters2)


#SECTION D.1 TREATMENT BELIEVABILITY & MANIPULATION CHECKS

#Treatment believability
final_data_IO %>% summarize(mean(heardabout_dummy, na.rm = TRUE))
final_data_IO %>% group_by(attack) %>% summarize(mean(heardabout_dummy, na.rm = TRUE))

#Manipulation check
final_data_IO %>% group_by(attack) %>% summarize(mean(manipcheck1, na.rm = TRUE))
final_data_IO %>% group_by(attack) %>% summarize(mean(manipcheck2, na.rm = TRUE))
final_data_IO %>% group_by(attack) %>% summarize(mean(fail_neither, na.rm = TRUE))
final_data_IO %>% summarize(mean(fail_neither, na.rm = TRUE))


#SECTION D.2 TREATMENT UPTAKE IN THE AEMT

#Manipulation checks of self reported emotions
reg_ANIEanger <- lm(anger ~ emotion + pid_group_merged, data=filter(final_data_IO, newsstory!="None"))
print(reg_ANIEanger)
summary(reg_ANIEanger)

reg_ANIEfear <- lm(fear ~ emotion + pid_group_merged, data=filter(final_data_IO, newsstory!="None"))
print(reg_ANIEfear)
summary(reg_ANIEfear)

reg_ANIErelax <- lm(relaxed ~ emotion, data=filter(final_data_IO, newsstory!="None"))
print(reg_ANIErelax)
summary(reg_ANIErelax)

#Table D.1: Effect of emotion treatments on self-reported emotions after terrorism news
stargazer(reg_ANIEanger, reg_ANIEfear, reg_ANIErelax, align=TRUE)

#Interaction of AEMT uptake and partisanship
reg_ANIEangerxPID <- lm(anger ~ emotion*pid_group_merged, data=filter(final_data_IO, newsstory!="None"))
print(reg_ANIEangerxPID)
summary(reg_ANIEangerxPID)

reg_ANIEfearxPID <- lm(fear ~ emotion*pid_group_merged, data=filter(final_data_IO, newsstory!="None"))
print(reg_ANIEfearxPID)
summary(reg_ANIEfearxPID)

stargazer(reg_ANIEangerxPID, reg_ANIEfearxPID, align=TRUE)

#Table D.2: Triple Interaction of AEMT on emotions by perpetrator and PID
reg_ANIEangerxPID2 <- lm(anger ~ emotion*pid_group_merged*attacker_id, data=filter(final_data_IO, newsstory!="None"))
print(reg_ANIEangerxPID2)
summary(reg_ANIEangerxPID2)

reg_ANIEfearxPID2 <- lm(fear ~ emotion*pid_group_merged*attacker_id, data=filter(final_data_IO, newsstory!="None"))
print(reg_ANIEfearxPID2)
summary(reg_ANIEfearxPID2)

stargazer(reg_ANIEangerxPID2, reg_ANIEfearxPID2, align=TRUE)


#SECTION E.1 SEM MODELS

#Figure E.1
model = 
  "
#Regressions
anger ~ a1*newsstory
punish ~ d21*anger +  a2*newsstory
retaliate ~ b2*punish + b1*anger + cp*newsstory + pid_group_merged_num + age_normal + male + white + christian + educ_normal + income_normal + 
  exposure_either_normal + trait_anxiety_normal + trait_anger_normal + RWA_normal + risk_accept_normal + NFC_normal + NFA_normal

#Parameters
ie := a1*d21*b2
de := cp

  "
fit <- lavaan::sem(model = model, data = subset(final_data_IO, nat_med == "1"), se = "boot", bootstrap = 1000, iseed=8675309)
summary(fit)

#confidence intervals
lavaan::parameterEstimates(fit, boot.ci.type = "bca.simple")


#Figure E.2
model1 = 
  "
#Regressions
fear ~ a1*newsstory
incapac ~ d21*fear +  a2*newsstory
retaliate ~ b2*incapac + b1*fear + cp*newsstory + pid_group_merged_num + age_normal + male + white + christian + educ_normal + income_normal + 
  exposure_either_normal + trait_anxiety_normal + trait_anger_normal + RWA_normal + risk_accept_normal + NFC_normal + NFA_normal

#Parameters
ie := a1*d21*b2
de := cp

  "

fit1 <- lavaan::sem(model = model1, data = subset(final_data_IO, nat_med == "1"), se = "boot", bootstrap = 1000, iseed = 8675309)
summary(fit1)

#confidence intervals
lavaan::parameterEstimates(fit, boot.ci.type = "bca.simple")


#SECTION E.2 MEDIATION MODELS

med.fit <- lm(anger ~ newsstory + age_normal + male + white + christian + educ_normal + income_normal + pid_group_merged + 
                exposure_either_normal + trait_anxiety_normal + trait_anger_normal + RWA_normal + risk_accept_normal + NFC_normal + NFA_normal, subset(final_data_IO, nat_med=="1"))
summary(med.fit)

out.fit <- lm(retaliate ~ anger + newsstory + age_normal + male + white + christian + educ_normal + income_normal + pid_group_merged + 
                exposure_either_normal + trait_anxiety_normal + trait_anger_normal + RWA_normal + risk_accept_normal + NFC_normal + NFA_normal, subset(final_data_IO, nat_med=="1"))

set.seed(1234)
med.out <- mediation::mediate(med.fit, out.fit, treat = "newsstory", mediator = "anger", robustSE = TRUE, sims = 1000)

#Figure E.3a
summary(med.out)

#Figure E.3b
set.seed(1234)
sens.out <- mediation::medsens(med.out, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out)

plot(sens.out, sens.par = "rho", main = "Anger", ylim = c(-0.3, 0.3))


#Placebo model with fear as mediator

#fear as mediator
med.fit1 <- lm(fear ~ newsstory + age_normal + male + white + christian + educ_normal + income_normal + pid_group_merged + 
                 exposure_either_normal + trait_anxiety_normal + trait_anger_normal + RWA_normal + risk_accept_normal + NFC_normal + NFA_normal, subset(final_data_IO, nat_med=="1"))
summary(med.fit1)

out.fit1 <- lm(retaliate ~ fear + newsstory + age_normal + male + white + christian + educ_normal + income_normal + pid_group_merged + 
                 exposure_either_normal + trait_anxiety_normal + trait_anger_normal + RWA_normal + risk_accept_normal + NFC_normal + NFA_normal, subset(final_data_IO, nat_med=="1"))

set.seed(1234)
med.out1 <- mediation::mediate(med.fit1, out.fit1, treat = "newsstory", mediator = "fear", robustSE = TRUE, sims = 1000)

#Figure E.4a
summary(med.out1)


#Figure E.4b
set.seed(1234)
sens.out1 <- mediation::medsens(med.out1, rho.by = 0.1, effect.type = "indirect", sims = 1000)
summary(sens.out1)

plot(sens.out1, sens.par = "rho", main = "Fear", ylim = c(-0.3, 0.3))


#SECTION F: OTHER PREREGISTERED ANALYSES

#SECTION F.1: Indirect effect of anger: comparison to reassurance treatment

#Table F.1
reg_ANIEconciliate_reas <- lm(conciliate ~ emotion, data = subset(final_data_IO %>% filter(newsstory != "None") %>%
                                                                    mutate(emotion = fct_relevel(emotion, "Reassure")))
)
summary(reg_ANIEconciliate_reas)

reg_ANIEretaliate_reas <- lm(retaliate ~ emotion, data=subset(final_data_IO %>% filter(newsstory != "None") %>%
                                                                mutate(emotion = fct_relevel(emotion, "Reassure")))
)
summary(reg_ANIEretaliate_reas)

stargazer(reg_ANIEretaliate_reas, reg_ANIEconciliate_reas, align=TRUE)

#Table F.2
reg_ANIErehab_reas <- lm(rehab ~ emotion, data = (final_data_IO %>% filter(newsstory != "None") %>%
                                                    mutate(emotion = fct_relevel(emotion, "Reassure")))
)
summary(reg_ANIErehab_reas)

reg_ANIEincapac_reas <- lm(incapac ~ emotion, data = (final_data_IO %>% filter(newsstory != "None") %>%
                                                        mutate(emotion = fct_relevel(emotion, "Reassure")))
)
summary(reg_ANIEincapac_reas)

reg_ANIEdeter_reas <- lm(deter ~ emotion, data = (final_data_IO %>% filter(newsstory != "None") %>%
                                                    mutate(emotion = fct_relevel(emotion, "Reassure")))
)
summary(reg_ANIEdeter_reas)

reg_ANIEpunish_reas <- lm(punish ~ emotion, data = subset(final_data_IO %>% filter(newsstory != "None") %>%
                                                            mutate(emotion = fct_relevel(emotion, "Reassure")))
)
summary(reg_ANIEpunish_reas)

stargazer(reg_ANIErehab_reas, reg_ANIEincapac_reas, reg_ANIEdeter_reas, reg_ANIEpunish_reas, align=TRUE)


#SECTION F.2: Direct effects of emotions on attitudes

#Table F.3
reg_DEanger <- lm(anger ~ condition, data=subset(final_data_IO, newsstory == "None"))
summary(reg_DEanger)

reg_DEfear <- lm(fear ~ condition, data=subset(final_data_IO, newsstory == "None"))
summary(reg_DEfear)

reg_DErelax <- lm(relaxed ~ condition, data=subset(final_data_IO, newsstory == "None"))
summary(reg_DErelax)

stargazer(reg_DEanger, reg_DEfear, reg_DErelax, align=TRUE)


#Table F.4
reg_DEretaliate <- lm(retaliate ~ condition, data=subset(final_data_IO, newsstory == "None"))
summary(reg_DEretaliate)

reg_DEconciliate <- lm(conciliate ~ condition, data=subset(final_data_IO, newsstory == "None"))
summary(reg_DEconciliate)

stargazer(reg_DEretaliate, reg_DEconciliate, align=TRUE)


#Table F.5
reg_DEpunish <- lm(punish ~ condition, data=subset(final_data_IO, newsstory == "None"))
summary(reg_DEpunish)

reg_DEdeter <- lm(deter ~ condition, data=subset(final_data_IO, newsstory == "None"))
summary(reg_DEdeter)

reg_DEincapac <- lm(incapac ~ condition, data=subset(final_data_IO, newsstory == "None"))
summary(reg_DEincapac)

reg_DErehab <- lm(rehab ~ condition, data=subset(final_data_IO, newsstory == "None"))
summary(reg_DErehab)

stargazer(reg_DEpunish, reg_DEdeter, reg_DEincapac, reg_DErehab, align=TRUE)


#SECTION G TWITTER ANALYSIS

#SECTION G.4: Analysis

load(file = "twitter_data_IO.Rdata")

#Table G.2

tweet_data %>% 
  dplyr::select(Anxiety, Anger, Anxiety.Risk, Anger.Morality,attack, attackLabel, attackDate) %>% 
  pivot_longer(cols = c(Anxiety, Anger, Anxiety.Risk, Anger.Morality), names_to = "Content") %>% 
  group_by(attack, Content) %>% 
  summarise("avg" = mean(value), sd(value), "sem" = sd(value) / sqrt(n()))

#T-Test
tweet_data %>% 
  dplyr::select(Anxiety, Anger, attack, attackDate) %>% 
  pivot_longer(cols = c(Anxiety, Anger), names_to = "Emotion") %>% 
  group_by(attack) %>% 
  mutate(Date = lubridate::ymd(attackDate),
         Attack = attack) %>% 
  summarise("Mean Anger" = mean(value[Emotion == "Anger"]), 
            "SD Anger" = sd(value[Emotion == "Anger"]), 
            #"semAnger" = sd(value[Emotion == "Anger"]) / sqrt(n()),
            
            "Mean Fear" = mean(value[Emotion == "Anxiety"]), 
            "SD Fear" = sd(value[Emotion == "Anxiety"]), 
            #"semFear" = sd(value[Emotion == "Anxiety"]) / sqrt(n()),
            
            t.test_stat = t.test(paired = TRUE, value[Emotion == "Anxiety"], value[Emotion == "Anger"])$statistic,
            t.test_df = t.test(paired = TRUE, value[Emotion == "Anxiety"], value[Emotion == "Anger"])$parameter,
            t.test_pval = t.test(paired = TRUE, value[Emotion == "Anxiety"], value[Emotion == "Anger"])$p.value,
            #t.test_stderr = t.test(paired = TRUE, value[Emotion == "Anxiety"], value[Emotion == "Anger"])$stderr
            Date = max(attackDate)
  ) %>% 
  arrange(Date) %>%
  knitr::kable(format = "latex", digits = 2)


#Section G.5: Robustness to Dictionary Specification

#Figure G.1 (using additional moral + risk words with anger and fear, respectively)
#pdf(width = 10, height = 5, file = "tweets_bar_ci3.pdf")

tweet_data %>% 
  dplyr::select(Anxiety.Risk, Anger.Morality, attack, attackLabel, attackDate) %>% 
  pivot_longer(cols = c(Anxiety.Risk, Anger.Morality), names_to = "Emotion") %>% 
  mutate(Date = lubridate::ymd(attackDate)) %>% 
  mutate(attackLabel = fct_reorder(attackLabel, .x = attackDate, .fun = max)) %>% 
  ggplot(aes(x = attackLabel, y = value, fill = Emotion)) +
  stat_summary(geom = "bar", position = "dodge") +
  stat_summary(width = 0.2, 
               geom = "errorbar", 
               position = position_dodge(0.95), 
               fun.data = mean_cl_normal) +
  labs(y = "Avg. Tweet Emotion %", x = "Attack") +
  scale_fill_manual(name = "Emotion", 
                    labels = c("Anger", "Anxiety"), 
                    values = RColorBrewer::brewer.pal(11,"Spectral")[c(2, 11)]) +
  cowplot::theme_cowplot() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

#dev.off() 


#Figure G.2 (using LIWC2015)

tweet_data2 %>% 
  dplyr::select(anx, anger, sad, Incident) %>% 
  pivot_longer(cols = c(anx, anger, sad), names_to = "Emotion") %>% 
  group_by(Incident, Emotion) %>% 
  summarise("avg" = mean(value), sd(value), "sem" = sd(value) / sqrt(n()))


#pdf(width = 10, height = 5, file = "tweets_bar_ci.pdf")

tweet_data2 %>% 
  dplyr::select(anx, anger, Incident, Date) %>% 
  pivot_longer(cols = c(anx, anger), names_to = "Emotion") %>% 
  mutate(Date = lubridate::ymd(Date)) %>% 
  mutate(Incident = fct_reorder(Incident, .x = Date, .fun = max)) %>% 
  ggplot(aes(x = Incident, y = value, fill = Emotion)) +
  stat_summary(geom = "bar", position = "dodge") +
  stat_summary(width = 0.2, 
               geom = "errorbar", 
               position = position_dodge(0.95), 
               fun.data = mean_cl_normal) +
  labs(y = "Avg. Tweet Sentiment %", x = "Attack") +
  scale_fill_manual(name = "Emotion", 
                    labels = c("Anger", "Anxiety"), 
                    values = RColorBrewer::brewer.pal(11,"Spectral")[c(2, 11)]) +
  cowplot::theme_cowplot() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

#dev.off() 

#SECTION H: REGRESSION TABLES OF MAIN RESULTS

#Table H.1: Paired sample t-tests of reported anger and fear after news exposure (Figure 3a)
ttest_angerfear = subset(final_data_IO, nat_med == 1) %>% 
  dplyr::select(condition, anger, fear) %>%
  mutate(condition = case_when(
    condition == "True Control" ~ "No Attack",
    condition == "JC No Emotion" ~ "Jihadi-Civilian",
    condition == "JM No Emotion" ~ "Jihadi-Military",
    condition == "AC No Emotion" ~ "WN-Civilian",
    condition == "AM No Emotion" ~ "WN-Military"
  )) %>% 
  mutate(condition = fct_relevel(condition, "No Attack")) %>%
  pivot_longer(cols = -condition, names_to = "Emotion") %>% 
  mutate(Emotion = ifelse(Emotion == "fear", "Fear", "Anger")) %>% 
  group_by(condition) %>% 
  do(tidy(t.test(value~Emotion, paired = TRUE, data=.)))

subset(final_data_IO, nat_med == 1) %>% 
  dplyr::select(condition, anger, fear) %>%
  pivot_longer(cols = c(anger, fear), names_to = "Emotion") %>% 
  group_by(condition) %>% 
  summarise("avgAnger" = mean(value[Emotion == "anger"], na.rm=TRUE), 
            "sdAnger" = sd(value[Emotion == "anger"], na.rm=TRUE), 
            "semAnger" = sd(value[Emotion == "anger"], na.rm=TRUE) / sqrt(n()),
            
            "avgFear" = mean(value[Emotion == "fear"], na.rm=TRUE), 
            "sdFear" = sd(value[Emotion == "fear"], na.rm=TRUE), 
            "semFear" = sd(value[Emotion == "fear"], na.rm=TRUE) / sqrt(n()),
            
            t.test_stat = t.test(paired = TRUE, value[Emotion == "fear"], value[Emotion == "anger"])$statistic,
            t.test_pval = t.test(paired = TRUE, value[Emotion == "fear"], value[Emotion == "anger"])$p.value,
            t.test_stderr = t.test(paired = TRUE, value[Emotion == "fear"], value[Emotion == "anger"])$stderr
  ) %>% 
  knitr::kable(format = "latex")

#Table H.2:ATE of exposure to different attack news on anger and fear (Figure 3b)
reg_ATEanger <- lm(anger ~ condition, data=subset(final_data_IO, nat_med=="1"))
summary(reg_ATEanger)

reg_ATEfear <- lm(fear ~ condition, data=subset(final_data_IO, nat_med=="1"))
summary(reg_ATEfear)

stargazer(reg_ATEanger, reg_ATEfear, align=TRUE)


#Table H.3: Support for conciliation by terrorism type and partisanship

#model 1
reg_ATEconciliate <- lm(conciliate ~ newsstory, data=subset(final_data_IO, nat_med=="1"))
summary(reg_ATEconciliate)

#model 2
reg_ATEconciliate_victimid <- lm(conciliate ~ victim_id, data=filter(final_data_IO, nat_med =="1")%>% 
                                  mutate(victim_id = fct_relevel(victim_id, "Civilian")))
summary(reg_ATEconciliate_victimid)

#model 3
reg_ATEconciliate_attackerid <- lm(conciliate ~ attacker_id, data=filter(final_data_IO, nat_med =="1")%>% 
                                    mutate(attacker_id = fct_relevel(attacker_id, "Jihadist")))
summary(reg_ATEconciliate_attackerid)

#model 4
reg_INTconciliate_attackerid <- lm(conciliate ~ attacker_id*pid_group_merged, data=subset(final_data_IO, nat_med=="1")%>% 
                                    mutate(pid_group_merged = fct_relevel(pid_group_merged, "Democrat"))%>% 
                                    mutate(attacker_id = fct_relevel(attacker_id, "Jihadist")))
summary(reg_INTconciliate_attackerid)

#model 5
reg_INTconciliate_victimid <- lm(conciliate ~ victim_id*pid_group_merged, data=subset(final_data_IO, nat_med=="1")%>% 
                                  mutate(pid_group_merged = fct_relevel(pid_group_merged, "Democrat"))%>% 
                                  mutate(victim_id = fct_relevel(victim_id, "Civilian")))
summary(reg_INTconciliate_victimid)

stargazer(reg_ATEconciliate, reg_ATEconciliate_victimid, reg_ATEconciliate_attackerid, reg_INTconciliate_attackerid, reg_INTconciliate_victimid, align=TRUE)


#Table H.4: Punitive motives by terrorism type and partisanship

#model 1
reg_ATEpunitive <- lm(punish ~ newsstory, data=subset(final_data_IO, nat_med=="1"))
summary(reg_ATEpunitive)

#model 2
reg_ATEpunitive_victimid <- lm(punish ~ victim_id, data=filter(final_data_IO, nat_med =="1")%>% 
                                   mutate(victim_id = fct_relevel(victim_id, "Civilian")))
summary(reg_ATEpunitive_victimid)

#model 3
reg_ATEpunitive_attackerid <- lm(punish ~ attacker_id, data=filter(final_data_IO, nat_med =="1")%>% 
                                     mutate(attacker_id = fct_relevel(attacker_id, "Jihadist")))
summary(reg_ATEpunitive_attackerid)

#model 4
reg_INTpunitive_attackerid <- lm(punish ~ attacker_id*pid_group_merged, data=subset(final_data_IO, nat_med=="1")%>% 
                                     mutate(pid_group_merged = fct_relevel(pid_group_merged, "Democrat"))%>% 
                                     mutate(attacker_id = fct_relevel(attacker_id, "Jihadist")))
summary(reg_INTpunitive_attackerid)

#model 5
reg_INTpunitive_victimid <- lm(punish ~ victim_id*pid_group_merged, data=subset(final_data_IO, nat_med=="1")%>% 
                                   mutate(pid_group_merged = fct_relevel(pid_group_merged, "Democrat"))%>% 
                                   mutate(victim_id = fct_relevel(victim_id, "Civilian")))
summary(reg_INTpunitive_victimid)

stargazer(reg_ATEpunitive, reg_ATEpunitive_victimid, reg_ATEpunitive_attackerid, reg_INTpunitive_attackerid, reg_INTpunitive_victimid, align=TRUE)


#Table H.5: Anger and punitive motives by perpetrator identity and partisanship (Figure 4)
reg_INTanger <- lm(anger ~ attacker_id*pid_group_merged, data=subset(final_data_IO, nat_med=="1")%>% 
                     mutate(pid_group_merged = fct_relevel(pid_group_merged, "Democrat"))%>% 
                     mutate(attacker_id = fct_relevel(attacker_id, "Jihadist")))
summary(reg_INTanger)

reg_INTpunish <- lm(punish ~ attacker_id*pid_group_merged, data=subset(final_data_IO, nat_med=="1")%>% 
                      mutate(pid_group_merged = fct_relevel(pid_group_merged, "Democrat"))%>% 
                      mutate(attacker_id = fct_relevel(attacker_id, "Jihadist")))
summary(reg_INTpunish)

stargazer(reg_INTanger, reg_INTpunish, align = TRUE)


#Table H.6: Indirect effect of emotions on political attitudes (Figure 5)
reg_ANIEretaliate_noprime <- lm(retaliate ~ emotion, data=subset(final_data_IO %>% filter(newsstory != "None")))
summary(reg_ANIEretaliate_noprime)

reg_ANIEconciliate_noprime <- lm(conciliate ~ emotion, data = subset(final_data_IO %>% filter(newsstory != "None")))
summary(reg_ANIEconciliate_noprime)

stargazer(reg_ANIEretaliate_noprime, reg_ANIEconciliate_noprime, align=TRUE)


#Table H.7: Indirect effect of emotions on motives for retaliation (Figure 6)
reg_ANIErehab <- lm(rehab ~ emotion, data = subset(final_data_IO %>% filter(newsstory != "None")))
summary(reg_ANIErehab)

reg_ANIEincapac <- lm(incapac ~ emotion, data = subset(final_data_IO %>% filter(newsstory != "None")))
summary(reg_ANIEincapac)

reg_ANIEdeter <- lm(deter ~ emotion, data = subset(final_data_IO %>% filter(newsstory != "None")))
summary(reg_ANIEdeter)

reg_ANIEpunish <- lm(punish ~ emotion, data = subset(final_data_IO %>% filter(newsstory != "None")))
summary(reg_ANIEpunish)

stargazer(reg_ANIErehab, reg_ANIEincapac, reg_ANIEdeter, reg_ANIEpunish, align=TRUE)


#Table H.8: Interaction of party ID and perpetrator Id on attitudes (no emotion prime, Figure 7)
reg_ACDE_retaliate <- lm(retaliate ~ pid_group_merged*attacker_id, data = subset(final_data_IO, emotion == "None")%>% 
                           mutate(attacker_id = fct_relevel(attacker_id, "Jihadist")))
summary(reg_ACDE_retaliate)

reg_ACDE_punish <- lm(punish ~ pid_group_merged*attacker_id, data = subset(final_data_IO, emotion == "None")%>% 
                        mutate(attacker_id = fct_relevel(attacker_id, "Jihadist")))
summary(reg_ACDE_punish)

stargazer(reg_ACDE_punish, reg_ACDE_retaliate, align=TRUE)


#Table H.9: Interaction of party ID and perpetrator Id on attitudes (anger prime, Figure 7)
reg_ACDE_retaliate1 <- lm(retaliate ~ pid_group_merged*attacker_id+RWA_normal+NFC_normal+trait_anger_normal, data = subset(final_data_IO, emotion == "Anger")%>% 
                            mutate(attacker_id = fct_relevel(attacker_id, "Jihadist")))
summary(reg_ACDE_retaliate1)

reg_ACDE_punish1 <- lm(punish ~ pid_group_merged*attacker_id, data = subset(final_data_IO, emotion == "Anger")%>% 
                         mutate(attacker_id = fct_relevel(attacker_id, "Jihadist")))
summary(reg_ACDE_punish1)

stargazer(reg_ACDE_punish1, reg_ACDE_retaliate1, align=TRUE)

